using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using Thrift;

using Thrift.Transport;
using CtrlThrApi_CSharp; // Namespace for HelloWorldClient

namespace WinFormsApp1ThriftDemo;

public partial class Form1 : Form
{
    private CtrlThrApiClientWrapper _thriftClient;
    // 新增IP和端口成员变量
    private readonly string _serverIp = "192.168.2.31";
    private readonly int _serverPort = 19090;
    
    public Form1()
    {
        InitializeComponent();
        _thriftClient = new CtrlThrApiClientWrapper();
    }
    
    // Button click event handler to call the Thrift client Say method
    // 修改所有ConnectAsync调用，使用成员变量
    
    private async void buttonForceReconnect_Click(object sender, EventArgs e)
    {
        try
        {
            _thriftClient?.Dispose();
            _thriftClient = new CtrlThrApiClientWrapper();
            await _thriftClient.ConnectAsync(_serverIp, _serverPort);
            MessageBox.Show("强制重连成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {
            MessageBox.Show($"强制重连失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    private async void button1_Click(object sender, EventArgs e)
    {
        bool success = await _thriftClient.TrySay("Hello from Windows Forms!");
        MessageBox.Show(success ? "Message sent successfully!" : "Failed to send message", 
                       success ? "Success" : "Error", 
                       MessageBoxButtons.OK, 
                       success ? MessageBoxIcon.Information : MessageBoxIcon.Error);
    }
    
   
    
    // Properly override the Dispose method to dispose of both components and Thrift client
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            // Dispose the Thrift client
            _thriftClient?.Dispose();
            
            // Dispose the components container
            if (components != null)
            {
                components.Dispose();
            }
        }

        base.Dispose(disposing);
    }
    
    private async void buttonPing_Click(object sender, EventArgs e)
    {
        bool isAlive = await _thriftClient.TryPing();
        MessageBox.Show(isAlive ? "服务器响应正常" : "服务器无响应", 
                      "Ping结果", 
                      MessageBoxButtons.OK, 
                      isAlive ? MessageBoxIcon.Information : MessageBoxIcon.Warning);
    }
    
    private async void buttonLogin_Click(object sender, EventArgs e)
    {
        var user = new ThUserInfo { 
            UserName = "user",
            Password = "1", 
            Account = 1 
        };
        
        var (success, loginResult) = await _thriftClient.TryLoginController(user, "V5.1");
        
        if(success)
        {
            if (0==loginResult.ReturnFlag)
            {
                MessageBox.Show($"登录成功，Token: {loginResult.Token}", 
                    "成功", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show($"登录失败，密码或版本错误，Token: {loginResult.Token}", 
                    "错误", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Information);
            }
            
        }
        else
        {
            MessageBox.Show("登录失败", 
                           "错误", 
                           MessageBoxButtons.OK, 
                           MessageBoxIcon.Error);
        }
    }
    
    private async void buttonServoon_Click(object sender, EventArgs e)
    {
        bool success = await _thriftClient.TrySetManualServon(0, 1);
        MessageBox.Show(success ? "伺服开启成功" : "伺服开启失败", 
                       success ? "成功" : "错误", 
                       MessageBoxButtons.OK, 
                       success ? MessageBoxIcon.Information : MessageBoxIcon.Error);
    }
    
    private async void buttonServoOff_Click(object sender, EventArgs e)
    {
        bool success = await _thriftClient.TrySetManualServon(0, 0);
        MessageBox.Show(success ? "伺服关闭成功" : "伺服关闭失败", 
                       success ? "成功" : "错误", 
                       MessageBoxButtons.OK, 
                       success ? MessageBoxIcon.Information : MessageBoxIcon.Error);
    }

    private async void buttonClearMsg_Click(object sender, EventArgs e)
    {
        bool success = await _thriftClient.TrySetAllMsgClear();
        MessageBox.Show(success ? "清除成功" : "清除失败", 
                       success ? "成功" : "错误", 
                       MessageBoxButtons.OK, 
                       success ? MessageBoxIcon.Information : MessageBoxIcon.Error);
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
        throw new System.NotImplementedException();
    }
}